package com.yh.correct.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.yh.correct.model.CommunityAdmin;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.ICommunityAdminService;
import com.yh.correct.service.impl.InterviewPersonnelServiceImpl;
  
/**
 * 作者：胡文恒 
 * 创建时间:2017-03-08
 * 描述:社区刑人员管理控制器
 */
@RestController
@RequestMapping("/cAdmin")
@PropertySource(value = "classpath:config/system.properties")
public class CommunityAdminController {
	private static Logger logger = LogManager.getLogger(CommunityAdminController.class);
	@Autowired
	private ICommunityAdminService casi;
	@Value("${saveDire}")
	private String saveDire;
	@Autowired
	private InterviewPersonnelServiceImpl iipl;

	/**
	 * 进入档案详情页
	 * @param page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(){
		return new ModelAndView("community/communityAdmin");
	}


	/**
	 * 作者：胡文恒 
	 * 创建时间:2017-03-11 
	 * 描述: 查询所有社区刑人员
	 * @throws Exception 
	 */
	@RequestMapping("/get")
	public ResultData get(String name, int pageSize, int pageNum) throws Exception {
		Map<String, Object> cont = new HashMap<String, Object>();
		cont.put("pageSize", pageSize);
		cont.put("pageNum", pageNum);
		if (name == null) {
			name = "";
		}
		cont.put("name", name);
		Page p = casi.getLikeAdminCount(cont);
		ResultData rd = new ResultData();
		if (p != null) {
			List<Object> e = new ArrayList();
			e.add(p);
			rd.setCode(0);
			rd.setMessage("查询成功！");
			rd.setData(e);
		}
		return rd;
	}

	/**
	 * 作者：胡文恒 
	 * 创建时间:2017-03-08 
	 * 描述:根据id删除社区刑人员
	 * @throws Exception 
	 */
	@RequestMapping("/del")
	public ResultData del(int id) throws Exception {
		CommunityAdmin ca = new CommunityAdmin();
		ca.setId(id);
		int i = casi.delCommunityAdmin(ca);
		ResultData rd = new ResultData();
		if (i > 0) {
			rd.setCode(0);
			rd.setMessage("删除成功！");
		} else {
			rd.setCode(-1);
			rd.setMessage("删除失败！");
		}
		return rd;
	}

	/**
	 * 作者：胡文恒
     * 创建时间:2017-03-12
     * 描述:根据id查询详情
	 * @throws Exception 
	 */
	@RequestMapping("/getId")
	public ModelAndView get(int id) throws Exception{
		CommunityAdmin ca = new CommunityAdmin();
		ca.setId(id);
		Map cab = casi.getCommunityAdmin(ca);
		logger.debug(cab);
		return new ModelAndView("community/list", cab);
		 
	}

	/**
	 * 作者：胡文恒
     * 创建时间:2017-03-14
     * 描述:下载档案
	 * @throws Exception 
	 */
	@RequestMapping("/down")
	public void down(String filename,int id, HttpServletResponse resp) throws Exception {
		logger.debug(saveDire +id+"\\"+ filename);
		// 设置ContentType
		resp.setContentType("application/octet-stream; charset=utf-8");
		// 处理中文文件名中文乱码问题
		// String fileName = new String(file.getName().getBytes("utf-8"),
		// "ISO-8859-1");
		resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
		try {
			IOUtils.copy(new FileInputStream(new File(saveDire+"\\"+id, filename)), resp.getOutputStream());
			CommunityAdmin ca = new CommunityAdmin();
			ca.setId(id); 
			ca.setDowload_date(new Date());
			casi.update(ca);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 作者：胡文恒
     * 创建时间:2017-03-14
     * 描述:添加档案
	 * @throws Exception 
	 */
	@RequestMapping("/upload")
	public void upload(List<MultipartFile> files,CommunityAdmin ca,String name) throws Exception{
		logger.debug(files);
		Integer id = casi.getLastId();
		logger.debug(id);
		logger.debug(ca.getRecord_date());
		List<String> obj = new ArrayList<String>();
		File fe = new File(saveDire+"\\"+id+"\\");  //上传路径
		if(!fe.exists()){  //判断路径是否存在
			fe.mkdir();
		}   
		for(MultipartFile a:files){
			try {
				a.transferTo(new File(fe,a.getOriginalFilename()));//a.getOriginalFilename()取文件名
				logger.debug(a.getOriginalFilename());
				obj.add(a.getOriginalFilename());
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		logger.debug(obj);
		ca.setTm1(obj.get(0));
		ca.setTm2(obj.get(1));
		ca.setTm3(obj.get(2));
		ca.setTm4(obj.get(3));
		ca.setTm5(obj.get(4));
		ca.setTm6(obj.get(5));
		ca.setTm7(obj.get(6));
		ca.setTm8(obj.get(7));
		ca.setTm9(obj.get(8));
		ca.setTm10(obj.get(9));
		casi.addCommunityAdmin(ca);
	}

	/**
	 * 作者：胡文恒
     * 创建时间:2017-03-14
     * 描述:查询所有矫正人员
	 * @throws Exception 
	 */
	@RequestMapping("/add")
	public ModelAndView getAll() throws Exception{
		List<Object> list = iipl.getAllCorr();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("corr", list);
		return new ModelAndView("community/add",map);
	}
}
